package com.hksj.sglm.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.base.query.BaseDeviceQuery;
import com.hksj.base.query.BaseSpecialityQuery;
import com.hksj.base.service.DefaultAlarmService;
import com.hksj.base.service.DefaultDeviceService;
import com.hksj.base.service.DefaultProductService;
import com.hksj.base.vo.AlarmRecordVO;
import com.hksj.base.vo.DeviceStatusVO;
import com.hksj.base.vo.PropertyVO;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import com.hksj.common.core.enums.DeviceState;
import com.hksj.common.core.exception.ApplicationException;
import com.hksj.sglm.mapper.SpecialGasAlarmMapper;
import com.hksj.sglm.query.SpecialGasDailyAlarmQuery;
import com.hksj.sglm.query.SpecialGasDeviceQuery;
import com.hksj.sglm.vo.SpecialGasDailyAlarmNumVO;
import com.hksj.sglm.vo.SpecialGasDeviceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SpecialGasAlarmService {
    @Autowired
    private DefaultAlarmService defaultAlarmService;
    @Autowired
    private SpecialGasAlarmMapper specialGasAlarmMapper;
    @Autowired
    private DefaultProductService defaultProductService;
    @Autowired
    private DefaultDeviceService defaultDeviceService;

    public List<AlarmRecordVO> listCurrentAlarm(BaseSpecialityQuery query) {
        return defaultAlarmService.listCurrentAlarm(query);
    }

    public List<SpecialGasDailyAlarmNumVO> listDailyAlarmNum(SpecialGasDailyAlarmQuery query) {
        Date now = new Date();
        if (query.getDayNum() <= 0){
            throw new ApplicationException("传入的查询天数不正确");
        }
        DateTime start = DateUtil.beginOfDay(DateUtil.offsetDay(now, 1 - query.getDayNum()));
        String startTimeStr = DateUtil.formatDateTime(start);
        query.setStartTime(startTimeStr);
        query.setEndTime(DateUtil.formatDateTime(now));
        // 查询气体泄漏报警数
        Map<String,Integer> leakageAlarmNumMap = new HashMap<>();
        List<String> classifiedIdList = defaultProductService.listClassifiedIdByKey(SpecialityModelCodeEnum.Teqxl_product.getCode(), false);
        if (!ObjectUtils.isEmpty(classifiedIdList)){
            query.setClassifiedIdList(classifiedIdList);
            List<SpecialGasDailyAlarmNumVO> leakageAlarmNumList = specialGasAlarmMapper.listDailyAlarmNum(query);
            if (!ObjectUtils.isEmpty(leakageAlarmNumList)){
                leakageAlarmNumMap = leakageAlarmNumList.stream().collect(Collectors.toMap(SpecialGasDailyAlarmNumVO::getDay,SpecialGasDailyAlarmNumVO::getNum));
            }
        }

        // 查询气体压力监测设备报警数
        Map<String,Integer> pressureAlarmNumMap = new HashMap<>();
        classifiedIdList = defaultProductService.listClassifiedIdByKey(SpecialityModelCodeEnum.Qityl_product.getCode(),true);
        if (!ObjectUtils.isEmpty(classifiedIdList)){
            query.setClassifiedIdList(classifiedIdList);
            List<SpecialGasDailyAlarmNumVO> pressureAlarmNumList = specialGasAlarmMapper.listDailyAlarmNum(query);
            if (!ObjectUtils.isEmpty(pressureAlarmNumList)){
                pressureAlarmNumMap = pressureAlarmNumList.stream().collect(Collectors.toMap(SpecialGasDailyAlarmNumVO::getDay,SpecialGasDailyAlarmNumVO::getNum));
            }
        }

        List<SpecialGasDailyAlarmNumVO> res = new ArrayList<>();
        List<DateTime> rangeDayList = DateUtil.rangeToList(start, now, DateField.DAY_OF_MONTH);
        if (!ObjectUtils.isEmpty(rangeDayList)){
            for (DateTime dateTime : rangeDayList) {
                SpecialGasDailyAlarmNumVO specialGasDailyAlarmNumVO = new SpecialGasDailyAlarmNumVO();
                String day = DateUtil.formatDate(dateTime);
                specialGasDailyAlarmNumVO.setDay(day);
                specialGasDailyAlarmNumVO.setLeakageAlarmNum(leakageAlarmNumMap.getOrDefault(day,0));
                specialGasDailyAlarmNumVO.setPressureAlarmNum(pressureAlarmNumMap.getOrDefault(day,0));
                res.add(specialGasDailyAlarmNumVO);
            }
        }
        return res;
    }

    public Page<SpecialGasDeviceVO> pageDevice(SpecialGasDeviceQuery query) {
        List<String> classifiedIdList = new ArrayList<>();
        if (query.getType() == 1){
            // 气体泄漏
            classifiedIdList = defaultProductService.listClassifiedIdByKey(SpecialityModelCodeEnum.Teqxl_product.getCode(),false);
        }else{
            // 气体压力监测
            classifiedIdList = defaultProductService.listClassifiedIdByKey(SpecialityModelCodeEnum.Qityl_product.getCode(),true);
        }
        Page<SpecialGasDeviceVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setRecords(new ArrayList<>());
        if (!ObjectUtils.isEmpty(classifiedIdList)){
            query.setClassifiedIdList(classifiedIdList);
            List<String> deviceIdList = specialGasAlarmMapper.pageDeviceId(query,query.getDisAblePage() ? null : page);
            if (!ObjectUtils.isEmpty(deviceIdList)){
                BaseDeviceQuery baseDeviceQuery = new BaseDeviceQuery();
                baseDeviceQuery.setDeviceIdList(deviceIdList);
                baseDeviceQuery.setWithAlarm(1);
                List<DeviceStatusVO> list =  defaultDeviceService.getDefaultDeviceMapper().pageDevice(baseDeviceQuery);
                if (!ObjectUtils.isEmpty(list)){
                    List<SpecialGasDeviceVO> res = new ArrayList<>();
                    for (DeviceStatusVO deviceStatusVO : list) {
                        SpecialGasDeviceVO specialGasDeviceVO = new SpecialGasDeviceVO();
                        BeanUtils.copyProperties(deviceStatusVO,specialGasDeviceVO);
                        if (Objects.equals(deviceStatusVO.getState(), DeviceState.online.getValue())){
                            if (!ObjectUtils.isEmpty(deviceStatusVO.getPropertyVOList())){
                                PropertyVO tPropertyVO = deviceStatusVO.getPropertyVOList().get(0);
                                specialGasDeviceVO.setPropertyValue(tPropertyVO.getValue());
                                if (!ObjectUtils.isEmpty(tPropertyVO.getAlarmStatus())){
                                    specialGasDeviceVO.setAlarmStatus(tPropertyVO.getAlarmStatus());
                                }
                            }
                        }
                        res.add(specialGasDeviceVO);
                    }
                    defaultDeviceService.wrapAddr(res);
                    page.setRecords(res);
                }
            }
        }
        return page;
    }
}
